package com.rainng.coursesystem.controller.admin;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.module.task.dto.response.CarProjectResDTO;
import com.rainng.coursesystem.module.task.entity.projectEntity;
import com.rainng.coursesystem.module.task.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "获取VIN车所需要做的项目接口")
@RequestMapping("/v2/car")
@RestController
public class carProjectController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/{taskcode}/{vin}")
    @ApiOperation(value = "删除批次")
    public ResultVO deleteBatches(@PathVariable("taskcode") String taskcode,@PathVariable("vin") String vin){
//        return super.result(batchService.removeById(id));
        ResultVO re=new ResultVO<>();
        List<CarProjectResDTO> list= taskService.getCarprojectList(taskcode,vin);
        if(list.size()>0){
            CarProjectResDTO car= (CarProjectResDTO)list.get(0);
            //获取配置列表里面car的key值
            Integer carindex= Integer.parseInt(car.getAllConfigIndex());
            String veType=car.getVeType();
            JSONArray ja=new JSONArray(car.getConfigs());
            List<Map<String,String>> projectLis=new ArrayList<>();
            for(Object ob:ja){
                JSONObject item=new JSONObject(ob);
                Object key= item.get("key");
                if(key==carindex){

                    List lis2t=(List)item.get("pr");
                    for(Object i:lis2t){
                        Map<String,String> map=new HashMap<>();
                        Integer prjNum= Integer.parseInt(i.toString());
                        //
                       projectEntity prj=taskService.getProject(veType,prjNum);
                       map.put("id",prj.getId());
                       map.put("name",prj.getPrjFullName());
                       map.put("prjNum",prj.getPrjNum());
                       projectLis.add(map);
                    }
                }
            }

            return new ResultVO<>(200,"当前找到该车的项目",projectLis);
        }
        else{
            return new ResultVO<>(201,"当前找不到该车的项目",null);
        }
    }

}
